#include "data.h"
#include "ui_data.h"
#include <QBrush>

showdata::showdata(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::data)
{
    ui->setupUi(this);
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    ui->tableWidget->insertColumn(3);
    ui->tableWidget->insertColumn(4);
    ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tableWidget->verticalHeader()->setHidden(true);
    ui->tableWidget->setHorizontalHeaderItem(0, new QTableWidgetItem("Time"));
    ui->tableWidget->setHorizontalHeaderItem(1, new QTableWidgetItem("num"));
    ui->tableWidget->setHorizontalHeaderItem(2, new QTableWidgetItem("X"));
    ui->tableWidget->setHorizontalHeaderItem(3, new QTableWidgetItem("Z"));
    ui->tableWidget->setHorizontalHeaderItem(4, new QTableWidgetItem("Delta Z"));
}

showdata::~showdata()
{
    delete ui;
}

void showdata::setData(QVector<QStringList> datamap, double maxdeltaz)
{
    QStringList z0_line = datamap.at(0);
    int beginRow = 0;
    int rows = (datamap.size()>10)?10:datamap.size();
    if(rows == 10)
    {
        beginRow = datamap.size() - 10;
    }

    for(size_t i = 0; i < rows; i++)
    {
        QStringList linedata = datamap.at(i + beginRow);
        QString time = linedata.at(0);
        QString nb = linedata.at(1);
        QString x = linedata.at(3*id_-1);
        QString z = linedata.at(3*id_+1);
        QString z0 = z0_line.at(3*id_ + 1);
        double zd = z.toDouble();
        double z0d = z0.toDouble();
        double deltaz = zd - z0d;

        QString deltazstr = QString::number(deltaz);
        ui->tableWidget->insertRow(i);
        ui->tableWidget->setItem(i, 0, new QTableWidgetItem(time));
        ui->tableWidget->setItem(i, 1, new QTableWidgetItem(nb));
        ui->tableWidget->setItem(i, 2, new QTableWidgetItem(x));
        ui->tableWidget->setItem(i, 3, new QTableWidgetItem(z));
        if(maxdeltaz < deltaz)
        {
            QTableWidgetItem* item = new QTableWidgetItem(deltazstr);
            item->setBackground(QBrush(Qt::red));
            ui->tableWidget->setItem(i, 4, item);
        }
        else
        {
            QTableWidgetItem* item = new QTableWidgetItem(deltazstr);
            ui->tableWidget->setItem(i, 4, item);
        }

    }
}

void showdata::setID(int id)
{
    id_ = id;
}

void showdata::clearTable()
{
    ui->tableWidget->setRowCount(0);
}
